<?php
// 开启会话
session_start();

// 初始化角色数据
if (!isset($_SESSION['character'])) {
    $_SESSION['character'] = [
        'name' => '玩家',
        'energy' => 100, // 体力
        'knowledge' => 0, // 知识
        'money' => 0 // 金钱
    ];
}

// 处理玩家选择的活动
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $character = &$_SESSION['character'];

    switch ($action) {
        case 'study':
            if ($character['energy'] >= 20) {
                $character['energy'] -= 20;
                $character['knowledge'] += 10;
                $message = "你努力学习，知识增加了 10 点，体力减少了 20 点。";
            } else {
                $message = "体力不足，无法学习。";
            }
            break;
        case 'work':
            if ($character['energy'] >= 30) {
                $character['energy'] -= 30;
                $character['money'] += 50;
                $message = "你认真工作，赚到了 50 元，体力减少了 30 点。";
            } else {
                $message = "体力不足，无法工作。";
            }
            break;
        case 'rest':
            $character['energy'] += 30;
            if ($character['energy'] > 100) {
                $character['energy'] = 100;
            }
            $message = "你好好休息了一下，体力恢复了 30 点。";
            break;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟人生网页游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>模拟人生网页游戏</h1>
    <p>角色名称: <?php echo $_SESSION['character']['name']; ?></p>
    <p>体力: <?php echo $_SESSION['character']['energy']; ?></p>
    <p>知识: <?php echo $_SESSION['character']['knowledge']; ?></p>
    <p>金钱: <?php echo $_SESSION['character']['money']; ?></p>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '不足')!== false? 'error' : 'success'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <button type="submit" name="action" value="study">学习</button>
        <button type="submit" name="action" value="work">工作</button>
        <button type="submit" name="action" value="rest">休息</button>
    </form>
</body>

</html>